/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Units.attacks;

import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingSphere;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.HullCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.effect.shapes.EmitterSphereShape;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.util.TangentBinormalGenerator;
import mygame.Units.Unit;
import mygame.World;

/**
 *
 * @author Ricardo
 */
public class Cannon extends Weapon {
    
    Geometry projectil;
    Material mat_default;
    
    ParticleEmitter fire;
    
    public Cannon() {
        super(300, AttackType.PROJECTIL, DamageType.PHYSICAL, 10, 20, 3);
    }

    GhostControl ghost;
    public void build(AssetManager assetManager, PhysicsSpace physicsSpace, Node rootNode, Node shootables) {
        projectil = new Geometry("arrow", new Sphere(6, 6, 0.5f));
        mat_default =  new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_default.setColor("Color", ColorRGBA.Red); 
        projectil.setMaterial(mat_default);
        rootNode.attachChild(projectil);
        BoundingSphere bs=new BoundingSphere();
        ((Geometry)projectil).getMesh().setBound(bs);
        ((Geometry)projectil).getMesh().updateBound();
        
        
        ghost = new GhostControl(new SphereCollisionShape(0.01f));  // a box-shaped ghost
        projectil.addControl(ghost);                         // the ghost follows this node
        // attach everything to activate it
        rootNode.attachChild(projectil);
        physicsSpace.add(ghost);
        
        
        
        fire = 
            new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 15);
        Material mat_red = new Material(assetManager, 
                "Common/MatDefs/Misc/Particle.j3md");
        mat_red.setTexture("Texture", assetManager.loadTexture(
                "Textures/flame.png"));
        fire.setMaterial(mat_red);
        fire.setImagesX(2); 
        fire.setImagesY(2); // 2x2 texture animation
        fire.setEndColor(  new ColorRGBA(1f, 0f, 0f, 1f));   // red
        fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
        fire.setStartSize(1.5f);
        fire.setEndSize(0.1f);
        fire.setGravity(0, -1, 0);
        fire.setLowLife(0.5f);
        fire.setRotateSpeed(0.4f);
        fire.setHighLife(1);
        fire.getParticleInfluencer().setVelocityVariation(2);
        rootNode.attachChild(fire);
        
    }
    
    private float reloadTimer;
    private Vector3f firingLocation=new Vector3f();
    Unit target=null;
    Vector3f speed;
    
    private float colorTimer;
    boolean switchPojectilColor;
    
    boolean afterEffect;
    Geometry collisionGeometry;
    Vector3f collisionPoint=new Vector3f();
    public void update(float tpf) {
        reloadTimer+=tpf;
        colorTimer+=tpf;
        if(target!=null){
            if(ghost.getOverlappingCount()!=0){
                //collision
                for (PhysicsCollisionObject physicsCollisionObject : ghost.getOverlappingObjects()) {
                    Object object = physicsCollisionObject.getUserObject();
                    if(object instanceof Geometry && World.getPlayerByName(((Geometry)object).getName())!=null){
                        collisionGeometry = (Geometry) object;
                        projectil.setLocalScale(0);
                        collisionPoint.set(projectil.getLocalTranslation().subtract(collisionGeometry.getLocalTranslation()));
                        collisionPoint.add(speed.mult(tpf));
                        fire.getParticleInfluencer().setInitialVelocity(speed.normalizeLocal().multLocal(3));
                        fire.setLocalTranslation(projectil.getLocalTranslation());
                        fire.emitAllParticles();
                        fire.setParticlesPerSec(5);
                        target=null;
                        afterEffect=true;
                        break;
                    }
                }
            }
            projectil.move(speed.mult(tpf));
        }
        if(afterEffect){
            fire.getParticleInfluencer().setVelocityVariation(1);
            fire.getParticleInfluencer().setInitialVelocity(Vector3f.UNIT_XYZ);
            fire.setLocalTranslation(collisionGeometry.getLocalRotation().mult(collisionPoint).addLocal(collisionGeometry.getLocalTranslation()));
        }
        //blink
        if(colorTimer>0.3f){
            colorTimer=0;
            switchPojectilColor = !switchPojectilColor;
            if(switchPojectilColor) mat_default.setColor("Color", ColorRGBA.Red); 
            else mat_default.setColor("Color", ColorRGBA.Orange); 
        }
        if(reloadTimer>reloadTime){
            afterEffect=false;
//            firingLocation = owner.getLocation().add(owner.getRotation().mult(locationOffset));
            firingLocation.set(locationOffset);
            owner.getRotation().multLocal(firingLocation);
            firingLocation.addLocal(owner.getLocation());
            
            //find target
            target = findClosestTarget(firingLocation);
            if(target!=null){
                speed = firingLocation.subtract(target.getLocation()).normalizeLocal();
                speed.multLocal(projectilSpeed).negateLocal();
                projectil.setLocalTranslation(firingLocation);
                projectil.setLocalScale(1);
                reloadTimer=0;
            }
        }
        
       
    }

}
